﻿@using Blazorise.Docs.Models;
@using Blazorise.Docs.Options;
<BarItem>
    <BarDropdown>
        <BarDropdownToggle>@GetActiveVersionName()</BarDropdownToggle>
        <BarDropdownMenu>
            @foreach ( var version in AvailableVersions )
            {
                @if ( IsActiveUrl( version.BaseUrl ) )
                {
                    <BarDropdownItem To="@version.DocsUrl" Flex="Flex.InlineFlex.JustifyContent.Between.AlignItems.Center" Border="Border.Is1.OnTop.Is1.OnBottom">
                        <Span>@version.Name</Span>
                        @if ( IsActiveUrl( version.BaseUrl ) )
                        {
                            <Icon Name="IconName.Check" Padding="Padding.Is2.FromStart" TextColor="TextColor.Primary" />
                        }
                    </BarDropdownItem>
                }
                else
                {
                    <BarDropdownItem To="@version.DocsUrl">
                        @version.Name
                    </BarDropdownItem>
                }
            }
        </BarDropdownMenu>
    </BarDropdown>
</BarItem>
@code {
    private IEnumerable<DocsVersion> AvailableVersions => VersionOptions?.Versions ?? Enumerable.Empty<DocsVersion>();

    private string GetActiveVersionName() => AvailableVersions.FirstOrDefault( v => NavigationManager.Uri?.StartsWith( v.BaseUrl ) == true )?.Name;

    private bool IsActiveUrl( string url ) => NavigationManager.Uri?.StartsWith( url ) == true;

    [Inject] public NavigationManager NavigationManager { get; set; }

    [Inject] public DocsVersionOptions VersionOptions { get; set; }
}
